package com.demomaster.quickcompat.widget.radiobutton;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;

import com.demomaster.quickcompat.widget.base.QuickLayout;
import com.demomaster.quickcompat.widget.layout.QuickLinearLayout;

/**
 * @author: cgq
 * @time: 2025/03/06
 * @describe: 用来包含可选中控件
 */
public class QuickRadioButtonContainer extends QuickLinearLayout implements Checkable, QuickLayout {

    private int mCheckedId = NO_ID;

    public static interface OnCheckedChangeListener {
        void onCheckedChanged(QuickRadioButtonContainer buttonView, boolean isChecked);
    }
    OnCheckedChangeListener onCheckedChangeListener;

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    QuickRadioButtonContainer.OnCheckedChangeListener mChildOnCheckedChangeListener = new QuickRadioButtonContainer.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(QuickRadioButtonContainer buttonView, boolean isChecked) {
            boolean b = getChildCheckStatus(QuickRadioButtonContainer.this);
            if (isChecked() != b) {
                setChecked(b);
            }
        }
    };
    CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener2 = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            boolean b = getChildCheckStatus(QuickRadioButtonContainer.this);
            if (isChecked() != b) {
                setChecked(b);
            }
        }
    };
    public QuickRadioButtonContainer(Context context) {
        super(context);
        init(context, null, 0);
    }

    public QuickRadioButtonContainer(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public QuickRadioButtonContainer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        super.init(context, attrs, defStyleAttr);
        super.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setChecked(true);
                setSelected(true);
                checkChildView(QuickRadioButtonContainer.this);
                if (clickListener != null) {
                    clickListener.onClick(v);
                }
            }
        });
    }

    OnClickListener clickListener;
    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        this.clickListener = l;
    }

    private boolean getChildCheckStatus(ViewGroup viewGroup) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            if (viewGroup.getChildAt(i) instanceof Checkable && ((Checkable) viewGroup.getChildAt(i)).isChecked()) {
                return true;
            } else if (viewGroup.getChildAt(i) instanceof ViewGroup) {
                if (getChildCheckStatus((ViewGroup) viewGroup.getChildAt(i))) {
                    return true;
                }
            }
        }
        return false;
    }

    private void setChildrenCheckedListener(View child) {
        if (child instanceof Checkable) {
            // 设置子控件的ID
            if (child.getId() == View.NO_ID && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                child.setId(View.generateViewId());
            }
            // 设置选中状态
            if (((Checkable) child).isChecked()) {
                mCheckedId = child.getId();
                super.setChecked(true);
            }
        }

        // Set onCheckedChangeListener for AppCompatRadioButton and QuickRadioButtonContainer.
        if (child instanceof CompoundButton) {
            ((CompoundButton) child).setOnCheckedChangeListener(mChildOnCheckedChangeListener2);
        } else if (child instanceof QuickRadioButtonContainer) {
            ((QuickRadioButtonContainer) child).setOnCheckedChangeListener(mChildOnCheckedChangeListener);
        }else if (child instanceof ViewGroup) {
            ViewGroup parent = (ViewGroup) child;
            for (int i = 0; i < parent.getChildCount(); i++) {
                View childView = parent.getChildAt(i);
                setChildrenCheckedListener(childView); // Recursive call
            }
        }
    }

    private void checkView(ViewGroup viewGroup, int selectId) {
        mCheckedId = selectId;
        int count = viewGroup.getChildCount();
        if (viewGroup.getId() == selectId) {
            if (viewGroup instanceof QuickRadioButtonContainer) {
                ((QuickRadioButtonContainer) viewGroup).setChecked(true);
            }
            return;
        }
        for (int i = 0; i < count; i++) {
            View child = viewGroup.getChildAt(i);
            boolean isChecked = (selectId == child.getId());
            if (!isChecked) {
                if (child instanceof Checkable) {
                    ((Checkable) child).setChecked(false);
                    child.setSelected(false);
                } else {
                    if (child instanceof ViewGroup) {
                        checkView((ViewGroup) child, selectId);
                    }
                }
            }
            //QdLog.i("setChildState id="+((View)child).getId()+",sid="+selectId+",child="+child);
        }
    }
    private void checkChildView(View view) {
        if(view instanceof Checkable){
            ((Checkable) view).setChecked(true);
            view.setSelected(true);
        }
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
                checkChildView(child);
            }
        }
    }

    private void setChecked(ViewGroup viewGroup, boolean checked) {
        int count = viewGroup.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = viewGroup.getChildAt(i);
            if (child instanceof Checkable) {
                ((Checkable) child).setChecked(checked);
            } else if (child instanceof ViewGroup) {
                setChecked((ViewGroup) child, checked);
            }
        }
    }

    public void setChecked(boolean checked) {
        if (isChecked() != checked) {
            super.setChecked(checked);
            if (checked) {
                if (onCheckedChangeListener != null) {
                    ViewGroup parent = getRadioGroupByChildView(QuickRadioButtonContainer.this);
                    if (parent instanceof QuickRadioGroup) {
                        ((QuickRadioGroup) parent).check(this.getId());
                    }else if (parent instanceof QuickRadioButtonContainer) {
                        ((QuickRadioButtonContainer) parent).check(this.getId());
                    } else if (parent instanceof RadioGroup) {
                        ((RadioGroup) parent).check(this.getId());
                    }
                }
            } else {
                setChecked(this, false);
            }

            for(int i = 0; i < getChildCount(); i++){
                if(getChildAt(i) instanceof Checkable){
                    ((Checkable) getChildAt(i)).setChecked(checked);
                }else if(getChildAt(i) instanceof ViewGroup){
                    setChecked((ViewGroup) getChildAt(i), checked);
                }
            }
        }
    }
    public void check(@IdRes int id) {
        // don't even bother
        if (id != -1 && (id == mCheckedId)) {
            return;
        }
        mCheckedId = id;
        boolean b = getChildCheckStatus(this);
        setChecked(b);
//        if (mCheckedId != -1) {
//            setCheckedStateForView(mCheckedId, false);
//        }
//
//        if (id != -1) {
//            setCheckedStateForView(id, true);
//        }

        //setCheckedId(id);
    }
    @SuppressLint("ResourceType")
    @Override
    public void onViewAdded(View child) {
        super.onViewAdded(child);
        setChildrenCheckedListener(child);
    }

    private ViewGroup getRadioGroupByChildView(View view) {
        if (view.getParent() instanceof QuickRadioGroup) {
            return (ViewGroup) view.getParent();
        } else if (view.getParent() instanceof RadioGroup) {
            return (ViewGroup) view.getParent();
        } else if (view.getParent() instanceof ViewGroup) {
            return getRadioGroupByChildView((ViewGroup) view.getParent());
        }
        return null;
    }


    @Override
    public void toggle() {
    }
}
